package com.morpheus.zookeeper.cluster;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class OnEnableZookeeperClusterCondition extends SpringBootCondition {
	private static final Logger LOGGER = LoggerFactory.getLogger(OnEnableZookeeperClusterCondition.class);

	public OnEnableZookeeperClusterCondition() {
	}

	@Override
	public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
		Environment environment = context.getEnvironment();
		boolean containsCluster = environment.containsProperty("spring.zookeeper.connectionString")
				|| environment.containsProperty("spring.zookeeper.connection-string");
		boolean containsConnectionTimeoutMillis = environment
				.containsProperty("spring.zookeeper.connectionTimeoutMillis")
				|| environment.containsProperty("spring.zookeeper.connection-timeout-millis");
		ConditionOutcome conditionOutcome = new ConditionOutcome(containsCluster && containsConnectionTimeoutMillis,
				"connection-string and connection-timeout-millis is valid");
		return conditionOutcome;
	}
}
